<template>
  <div class="employees-container">
    <div class="app-container">
      <el-card>
        <!-- 放置表单 -->
        <el-form ref="userForm" label-width="120px" :model="userInfo" :rules="rules" style="margin-left: 120px; margin-top:30px">
          <el-form-item label="姓名:" prop="username">
            <el-input v-model="userInfo.username" style="width:300px" />
          </el-form-item>

          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" disabled style="width:300px" />
          </el-form-item>

          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>

          <el-form-item label="员工头像">
            <!-- <upload-img :url="userInfo.staffPhoto" @url-update="getUrl" /> -->
            <!-- v-model是语法糖 绑定 v-model会默认做以下三件事
                  1在组件身上绑定一个名称为 value的属性prop
                  2在组件身上绑定 一个名称为 input的自定义事件
                  3 当组件内部通过 this.$emit()去触发input事件时 事件参数会自动赋值给v-model的属性身上
             -->
            <upload-img v-model="userInfo.staffPhoto" />
          </el-form-item>
          <!-- 保存个人信息 -->
          <el-row class="inline-info" type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" @click="confirm">保存更新</el-button>
              <el-button @click="$router.back()">返回</el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
// 数据回显
/**
 * 1 准备接口
 * 2 路由方法获取id
 * 3 发生接口请求
 * 4 绑定的表单中
 */
import { getUserinforAPI,
  saveUserDetailById } from '@/api'

export default {
  name: 'EmployeesDetail',
  data() {
    return {
      userInfo: {},
      rules: {
        username: { required: true, message: '姓名不能为空', tiggter: 'bluer' }
      }
    }
  },
  mounted () {
    this.getUserinfo()
  },
  methods: {
    async getUserinfo() {
      const res = await getUserinforAPI(this.$route.query.id)
      this.userInfo = res.data
    },
    // 保存更新
    async confirm() {
      await saveUserDetailById(this.userInfo)
      this.$message.success('修改成功')
      this.$router.go(-1)
    }
    // 获取子组件的图片的url 传给父组件
    // getUrl(url) {
    //   this.userInfo.staffPhoto = url
    // }
  }
}
</script>
